package com.sjb.training.lc;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 有点儿意思
 * @Description:
 */
public class Student {

    private int id;
    private String  name;

    public Student(int id,String name){
        this.id = id;
        this.name = name;
    }

    public int getId(){
        return id;
    }

    public void setId(int id){
        this.id = id;
    }

    public String getName(String name){
        return name;
    }

    public String getName(){
        return name;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Student student = (Student) o;
        return id == student.id;
    }

    @Override
    public int hashCode() {
        return Integer.hashCode(id);
    }

    @Override
    public String toString() {
        return "Student{id=" + id + ", name='" + name + "'}";
    }

    public static List<Student>  removeDuplicateByName(List<Student> studentList){
        Set<String> students = new HashSet<>();
        List<Student> result = new ArrayList<>();
        for (Student student : studentList) {
            if (students.add(student.getName())){
                result.add(student);
            }
        }
        return result;
     /*   List<Student> studentList1 = studentList.stream().collect(Collectors.toMap(Student::getName, p -> p, (p1, p2) -> p1))
                .values().stream().collect(Collectors.toList());
        return studentList1;*/
    }

    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student(1,"sjb"));
        students.add(new Student(2,"sjb"));
        students.add(new Student(3,"wy"));
        students.add(new Student(4,"zr"));
        List<Student> duplicateByName = removeDuplicateByName(students);
        for (Student student : duplicateByName) {
            System.out.println(student);
        }
    }

}
